const Joi = require('joi');

class OrderValidate {

  async list(param) {

    const schema = Joi.object({
      name: Joi.string(),
      describe: Joi.string(),
      status: Joi.number(),
      order: Joi.string(),
      method: Joi.string(),
      pageIndex: Joi.string().default('1'),
      pageSize: Joi.string().default('10'),
    });

    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }


  async insert(param) {
    const schema = Joi.object({
      name: Joi.string()
        .required()
        .messages({
          'string.required': '名称是必填项',
          'string.empty': '名称为空',
        }),

      describe: Joi.string(),

      address_id: Joi.string()
        .required()
        .messages({
          'string.required': '地址是必填项',
          'string.empty': '地址不能为空',
        }),

      type: Joi.number()
        .required()
        .messages({
          'number.required': '类型必填项',
          'number.empty': '类型不能为空',
        }),

      level: Joi.number()
        .required()
        .messages({
          'number.required': '阶段是必填项',
          'number.empty': '阶段不能为空',
        }),

      status: Joi.number().required(),

      product: Joi.array().required(),
    });

    try {
      const value = await schema.validateAsync(param);
      return value;
    } catch (error) {
      throw error;
    }
  }


  async cancel(param) {
    const schema = Joi.object({
      order_id: Joi.string()
        .required()
        .messages({
          'string.required': '订单是必填项',
          'string.empty': '订单不能为空',
        }),

      remark: Joi.string(),
    });


    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }


  async finish(param) {
    const schema = Joi.object({
      order_id: Joi.string()
        .required()
        .messages({
          'string.required': '订单是必填项',
          'string.empty': '订单不能为空',
        }),
      remark: Joi.string()
        .required()
        .messages({
          'string.required': '备注是必填项',
          'string.empty': '备注不能为空',
        }),
    });


    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }

  async change(param) {
    const schema = Joi.object({
      address_id: Joi.string()
        .required()
        .messages({
          'string.required': '地址id是必填项',
          'string.empty': '地址id不能为空',
        }),

      order_id: Joi.string()
        .required()
        .messages({
          'string.required': '订单id是必填项',
          'string.empty': '订单id不能为空',
        }),
    });


    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }

  async refuse(param) {
    const schema = Joi.object({
      order_id: Joi.string()
        .required()
        .messages({
          'string.required': '订单id是必填项',
          'string.empty': '订单id不能为空',
        }),

      remark: Joi.string()
        .required()
        .messages({
          'string.required': '备注是必填项',
          'string.empty': '备注不能为空',
        }),
    });

    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }


  async delete(param) {
    const schema = Joi.object({
      ids: Joi.array().items(Joi.string()),
    });

    try {
      const value = await schema.validateAsync(param);

      return value;
    } catch (error) {
      throw error;
    }
  }

}

module.exports = new OrderValidate();
